#pragma once

#include <string>
#include <vector>
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>

class Util
{
public:
    static void StringSplit(std::vector<std::string>* result, const std::string& sep, const std::string& str)
    {
        // // 0123456
        // // 10 + 20
        size_t start = 0;
        size_t n = str.size();
        while(start < n)
        {
            size_t next = str.find(sep, start); // 2 3 5
            if(next == std::string::npos) break;

            result->push_back(str.substr(start, next-start));
            start = next + sep.size(); // 3 5
        }

        result->push_back(str.substr(start));     
    }

    static int ToInt(const std::string& str)
    {
        return atoi(str.c_str());
    }

    static void SetNonBlock(int fd)
    {
        int fl = fcntl(fd, F_GETFL);
        fcntl(fd, F_SETFL, fl | O_NONBLOCK);
    }
};